//
//  LottieAnimateBasicContentView.swift
//  DaRings
//
//  Created by 魔样科技 on 2022/10/27.
//

import Foundation
import Lottie
import ESTabBarController

class LottieAnimateBasicContentView: ESTabBarItemContentView {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        textColor = UIColor.init(hex: "3DCEC1")!
        highlightTextColor = UIColor.init(hex: "FC0D1B")!
        iconColor = UIColor.init(hex: "3DCEC1")!
        highlightIconColor = UIColor.init(hex: "FC0D1B")!
    }
    
    public required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

class LottieAnimateContentView: LottieAnimateBasicContentView {
    
    let lottieView: AnimationView = {
        let lottieView = AnimationView()
        lottieView.animation = Animation.named("loading", bundle: Bundle(for: LottieAnimateContentView.self))
        lottieView.contentMode = .scaleAspectFit // 填充模式
        lottieView.loopMode = .autoReverse // 动画播放次数
        lottieView.backgroundBehavior = .pauseAndRestore // 后台模式
        lottieView.play()
        return lottieView
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.addSubview(lottieView)
        self.lottieView.snp.makeConstraints { make in
            make.center.equalToSuperview()
            make.width.height.equalTo(20)
        }
    }
    
    func setAnimationName(_ animationName: String) {
        self.lottieView.animation = Animation.named(animationName, bundle: Bundle(for: LottieAnimateContentView.self))
        self.lottieView.play()
    }
    
    public required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func updateLayout() {
        super.updateLayout()
        lottieView.frame = self.bounds.insetBy(dx: -2, dy: -2)
    }
    
    override func selectAnimation(animated: Bool, completion: (() -> ())?) {
        super.selectAnimation(animated: animated, completion: nil)
        lottieView.pause()
    }
    
    override func deselectAnimation(animated: Bool, completion: (() -> ())?) {
        super.deselectAnimation(animated: animated, completion: nil)
        lottieView.play()
    }

}
